package com.ruoyi.web.controller.reserve;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reserve.domain.CheckGroup;
import com.ruoyi.reserve.domain.CheckgroupCheckitem;
import com.ruoyi.reserve.domain.Checkitem;
import com.ruoyi.reserve.service.impl.ICheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/reserve/checkGroup")
public class CheckGroupController extends BaseController {

    @Autowired
    private ICheckGroupService checkGroupService;



    /**
     * 获取检查项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CheckGroup checkGroup)
    {
        startPage();
        List<CheckGroup> list = checkGroupService.getList(checkGroup);
        TableDataInfo tableDataInfo = getDataTable(list);
        return tableDataInfo;
    }

    /**
     * 删除检查组
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Integer[] groupIds)
    {
        return toAjax(checkGroupService.deleteGroupByIds(groupIds));
    }

    /**
     * 新增检查组
     */
    @Log(title = "检查组管理", businessType = BusinessType.INSERT)
    @PostMapping("/addGroup")
    public AjaxResult add(@RequestBody CheckGroup checkGroup)
    {
        return toAjax(checkGroupService.insertGroup(checkGroup));
    }

    /**
     * 根据检查组id获取详情
     */
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo (@PathVariable Integer groupId) {
        return AjaxResult.success(checkGroupService.getInfo(groupId));
    }

    /**
     * 更新
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateGroup")
    public AjaxResult updateGroup(@RequestBody CheckGroup checkGroup)
    {
        CheckGroup checkgroupCode = checkGroupService.getInfoByCode(checkGroup.getCode());
        if (checkgroupCode != null ) {
            if(!checkGroup.getId().equals(checkgroupCode.getId())){
                return AjaxResult.error("该编号已存在");
            }
        }

        CheckGroup checkgroupName = checkGroupService.getInfoByCode(checkGroup.getName());
        if (checkgroupName != null ) {
            if(!checkGroup.getId().equals(checkgroupName.getId())){
                return AjaxResult.error("该名称已存在");
            }
        }
        return toAjax(checkGroupService.updateGroup(checkGroup));
    }

    /**
     * 删除检查组中的检查项
     */
    @Log(title = "检查组管理", businessType = BusinessType.DELETE)
    @PostMapping("/itemsid")
    public AjaxResult itemsid(@RequestBody List<CheckgroupCheckitem> checkgroupCheckitems)
    {
        Integer groupId = checkgroupCheckitems.get(0).getCheckgroupId();
        return toAjax(checkGroupService.deleteGroupByItemsByIds(groupId,checkgroupCheckitems));
    }
    /**
     * 查询可以被添加进检查组的检查项
     */
    @GetMapping("/getInfoByCheckGroup")
    public TableDataInfo getInfoByCheckGroup(CheckGroup checkGroup) {
        startPage();
        List<Checkitem> list = checkGroupService.getInfoByCheckGroup(checkGroup);
        return getDataTable(list);
    }
    /**
     * 新增检查组中的检查项
     */
    @Log(title = "检查组管理", businessType = BusinessType.INSERT)
    @PostMapping("/addCheckGroupItem")
    public AjaxResult addCheckGroupItems(@RequestBody List<CheckgroupCheckitem> checkgroupCheckitems)
    {
        return toAjax(checkGroupService.addCheckGroupItems(checkgroupCheckitems));
    }
}
